package cn.shadow.client;

import cn.shadow.proto.client.LandlordClientProtoContent;
import cn.shadow.utils.BufferUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;


public class NettyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    /**
     * 通道就绪，就会触发该方法
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        login(ctx);
    }

    private void login(ChannelHandlerContext ctx) throws InterruptedException {
        System.out.println("通道就绪");
        //向服务端发送消息，发送心跳信息
        ByteBuf byteBuf = BufferUtil.onlySendHeaderMessage(1, 3);
        BufferUtil.writeUTF(byteBuf, "100-1-a993751s75");
        ctx.writeAndFlush(byteBuf);
        System.out.println("登录完毕");
        Thread.sleep(2000);
        byteBuf = BufferUtil.onlySendHeaderMessage(1, 7);
        BufferUtil.writeBoolean(byteBuf, true);
        BufferUtil.writeUTF(byteBuf, "hero");
        ctx.writeAndFlush(byteBuf);
        Thread.sleep(2000);
        byteBuf = BufferUtil.onlySendHeaderMessage(2, 3);
        BufferUtil.writeVarInt32(byteBuf, 1);
        BufferUtil.writeVarInt32(byteBuf, 2);
        ctx.writeAndFlush(byteBuf);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            switch (event.state()) {
                case READER_IDLE:
                    System.out.println("读空闲");
                    break;
                case ALL_IDLE:
                    System.out.println("读写空闲");
                    break;
                case WRITER_IDLE:
                    System.out.println("写空闲");
                    break;
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        msg.readUnsignedByte();
        msg.readUnsignedByte();
        int modduleID = BufferUtil.readVarInt32(msg);
        System.out.println("modduleID = " + modduleID);
        int MsgID = BufferUtil.readVarInt32(msg);
        System.out.println("MsgID = " + MsgID);
        if (MsgID == 4 && modduleID == 2) {
            byte[] data = BufferUtil.readBytes(msg);
            LandlordClientProtoContent.Table table = LandlordClientProtoContent.Table.parseFrom(data);
            int id = table.getId();
            System.out.println("id = " + id);
            int count = table.getCount();
            System.out.println("count = " + count);
            String owner = table.getOwner();
            System.out.println("owner = " + owner);
            int type = table.getType();
            System.out.println("type = " + type);
        }
    }
}
